package com.yanftch.hdc_tools.command

import com.yanftch.hdc_tools.common.MessageDelegate
import com.yanftch.hdc_tools.common.executeCommand
import com.yanftch.hdc_tools.common.finalCommandLine
import com.yanftch.hdc_tools.common.isCommandSucceed
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking

class RestartAppCommand : Command {
    override fun run(commandContext: CommandContext): Boolean {
        with(commandContext) {
            runBlocking {
                val stop = async { executeCommand(finalCommandLine(hdcPath, force_stop, bundleName)) }.await()
                val start = async { executeCommand(finalCommandLine(hdcPath, hdc_aa_start(bundleName, defaultAbility))) }.await()
                if (stop.isCommandSucceed() && start.isCommandSucceed()) {
                    MessageDelegate.info("re-start app success.", action)
                } else {
                    MessageDelegate.error("re-start app failed. $stop: $start", action)
                }
            }
        }
        return false
    }
}